home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / edit.c < prev    next >
Text File  |  1990-07-30  |  25KB  |  625 lines

  1. /*              EDIT.C  - Screen Editor
  2.                 by Bill Kinnersley
  3.                 MSU Physics Dept
  4.                 January 1982
  5. EDIT has many of the conveniences of WordStar, but is a much smaller and
  6. faster program.  The program is presently limited to editing files of less
  7. than 44K total length, and lines shorter than 80 characters. */
  8. #include <stdioa.h>
  9. #include <iolib.asm>
  10. #include <call.asm>
  11. #define ESC 27
  12. #define BELL 07
  13. #define DC1 17
  14. #define DC3 19
  15. #define EOFCHAR 26
  16. char *topmem;                   /* pointer to top of memory */
  17. char *topfile;                  /* pointer to top of file */
  18. char *botfile;                  /* pointer to bottom of file */
  19. char *topscr;                   /* pointer to top of screen */
  20. char *botscr;                   /* pointer to bottom of screen */
  21. char *cursor;                   /* pointer to current cursor position in file */
  22. char *null;                     /* null pointer*/
  23. char *mark;                     /* temporary pointer */
  24. int lines;                      /* line # where botscr is (1 <= lines <=25) */
  25. int csr_col;                    /* current cursor column # */
  26. int want_col;                   /* cursor column desired */
  27. int csr_line;                   /* current cursor line # */
  28. int next_char;                  /* next input char, input during screen output*/
  29. int ch;                         /* current character input from keyboard */
  30. int flag;                       /* true if cursor==topscr */
  31. int itop;                       /* topfile as an integer */
  32. int ibot;                       /* botfile as an integer */
  33. char bufin[80];
  34. char fid[20];                   /* name of file to edit */
  35. char backup[20];                /* name of backup file */
  36. int file;                       /* file descriptor # used by C */
  37. int new;                        /* true if file was new */
  38. int i;                          /* number of records written to disk */
  39. main(argc,argv)
  40. int argc;                       /* number of arguments in command line */
  41. int argv[];
  42. {
  43.         topmem = CCAVAIL() - 4096;      /* get size of available memory */
  44.         botfile = topfile = botscr = topscr = cursor = CCALLOC(topmem);
  45.         topmem = botfile + topmem;      /* point to top of available memory */
  46.         null = "";
  47.         next_char = 0;
  48.         term_init();
  49.         if (argc == 1)          /* no command tail */
  50.         {       fputs("\nFILE? : ",stderr);     /* ask for fid */
  51.                 gets(fid);
  52.         }
  53.         else strcpy(fid,argv[1]);       /* fid from command line */
  54.         fid[14] = 0;                    /* trim to 14 chars */
  55.         new = - swapin(fid,topfile);            /* try to read in */
  56.         clear_screen();
  57.         csr_line = csr_col = want_col = 0;
  58.         if (new)
  59.         {       fputs("NEW FILE\n\n",stderr);   /* must be new file*/
  60.                 lines = 3;
  61.                 *botfile = EOFCHAR;
  62.                 csr_line = 2;
  63.         }
  64.         else
  65.         {       while (*botfile != EOFCHAR) botfile++;  /* look for botfile*/
  66.                 lines = 1;
  67.                 setbot();
  68.                 putscr(topfile);
  69.                 home();
  70.         }
  71.  
  72.         while(TRUE)
  73.         {       ch = get_conin();       /* wait for console input */
  74.                 if (ch == ESC ) ch = escape();  /* convert escape sequence */
  75.                 if (((ch >= ' ') && (ch < 127)) || (ch == '\t') || (ch == '\r'))
  76.                         ins_character();
  77.                 else if (special())     /* process special characters */
  78.                         return;         /* quit */
  79.         }
  80. }
  81. ins_character()
  82. /* insert character c at current cursor position */
  83. {       disable_keyboard();
  84.         e_eol();
  85.         ibot = ++botfile; itop = ++cursor;
  86.         ++botscr;
  87.         movmem(cursor-1,cursor,ibot-itop+1);
  88.         *(cursor-1) = ch;
  89.         if (ch == '\n')
  90.                 {where();
  91.                 csr_col = want_col = 0;
  92.                 if (csr_line == 23)
  93.                         {botscr = cursor;
  94.                         lines = 25;
  95.                         printbot();
  96.                         }
  97.                 else
  98.                         {putch('\n');
  99.                         setbot();
  100.                         ins_line();
  101.                         putline(cursor);
  102.                         csr_line++;
  103.                         put_cursor(csr_line,csr_col);
  104.                         }
  105.                 }
  106.         else
  107.                 {putch(ch);
  108.                 if (ch != '\t') csr_col++;
  109.                 else csr_col = ((csr_col>>3)+1)<<3;
  110.                 want_col = csr_col;
  111.                 putline(cursor);
  112.                 position();
  113.                 }
  114.         enable_keyboard();
  115. }
  116. special()
  117. /* procedure to process special characters */
  118. {               switch (ch) {
  119. /* cursor right */      case 'D'-64 :if (cursor == botfile) break;
  120.                                 else if ((ch=*cursor++)=='\n')
  121.                                         {printbot(); csr_col=0;}
  122.                                 else if (ch=='\t')
  123.                                         {putch('\t');
  124.                                         csr_col = ((csr_col>>3)+1)<<3;}
  125.                                 else {cup_right(); csr_col++;}
  126.                                 want_col = csr_col;
  127.                                 break;
  128. /* cursor left */       case 'S'-64 :
  129.                         case 'H'-64 : flag = (cursor==topscr);
  130.                                 if (cursor==topfile) break;
  131.                                 else if ((ch=*--cursor)=='\n')
  132.                                         {reverse_index();
  133.                                         if (flag)
  134.                                                 {topscr=startline(cursor);
  135.                                                 setbot();
  136.                                                 putch('\r');
  137.                                                 putline(topscr);}
  138.                                         position();
  139.                                         }
  140.                                 else if (ch=='\t') position();
  141.                                 else {putch('\b'); csr_col--;}
  142.                                 want_col = csr_col;
  143.                                 break;
  144. /* cursor up */         case 'E'-64 :
  145.                                 if ((mark=startline(cursor))==topfile) break;
  146.                                 cursor = startline(mark-1);
  147.                                 disable_keyboard();
  148.                                 reverse_index();
  149.                                 where();
  150.                                 if (mark==topscr) {topscr = cursor;
  151.                                                 setbot();
  152.                                                 cup_home();
  153.                                                 putline(topscr);}
  154.                                 set_cursor();
  155.                                 enable_keyboard();
  156.                                 break;
  157. /* cursor down */       case 'X'-64 :
  158.                         case 'J'-64 : if ((mark = next(cursor))==null) break;
  159.                                 cursor = mark;
  160.                                 printbot();
  161.                                 where();
  162.                                 set_cursor();
  163.                                 break;
  164. /* up screenful */      case 'R'-64 : if (topscr==topfile) break;
  165.                                 disable_keyboard();
  166.                                 where();
  167.                                 botscr = topscr;
  168.                                 settop();
  169.                                 newscr();
  170.                                 enable_keyboard();
  171.                                 break;
  172. /* down screenful */    case 'C'-64 : if (botscr==botfile) break;
  173.                                 disable_keyboard();
  174.                                 topscr = botscr;
  175.                                 setbot();
  176.                                 newscr();
  177.                                 enable_keyboard();
  178.                                 break;
  179. /* top file */          case 'T'-64 : if (topscr==topfile) break;
  180.